package com.openflag.clientservice.service.impl;

import com.openflag.clientservice.controller.ClientController;
import com.openflag.clientservice.domain.Client;
import com.openflag.clientservice.domain.GetClientUnit;
import com.openflag.clientservice.repository.ClientRepository;
import com.openflag.clientservice.service.ClientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Optional;

//帮助Spring找到我们这实现类，并且帮助实例化，注入Controller
@Service
public class ClientServiceImpl implements ClientService {

    @Autowired
    private ClientRepository clientRepository;

    private Logger LOG = LoggerFactory.getLogger(ClientController.class);
    @Override
    public GetClientUnit getClient(Long id) {
        Optional<Client> optionalClient = clientRepository.findById(id);
        GetClientUnit getClientUnit = new GetClientUnit();
        if(optionalClient.isPresent()){
            getClientUnit.setId(optionalClient.get().getId());
            getClientUnit.setAssetValue(optionalClient.get().getAssetValue());
            getClientUnit.setName(optionalClient.get().getName());
            return getClientUnit;
        }
        return null;
    }
    @Override
    public Optional<Client> loginClient(String account, String password){
        Client queryClient = new Client();
        queryClient.setAccount(account);
        queryClient.setPassword(password);

        Optional<Client> optionalClient = clientRepository.findOne(Example.of(queryClient));

        return optionalClient;



    }

    @Override
    public Integer registerClient(String name,String account,String password,Integer position){
        // 姓名不符合规范

        if(name.length()>30 || name.length()<2 || !name.matches("^[a-zA-Z0-9\\s]+$")){
            return 5;
        }

        //用户名不符合规则
        if(account.length()<8 || account.length()>16 || !account.matches("^[a-zA-Z0-9]*$")){
            return 2;
        }
        //密码不符合规则
        if(password.length()<8 || password.length()>16 || !password.matches("^[a-zA-Z0-9]*$")){
            return 3;
        }

        Client queryClient = new Client();

        Boolean ifExist = false;

        queryClient.setAccount(account);

        Optional<Client> optionalClient = clientRepository.findOne(Example.of(queryClient));
        // 用户名重名
        if (optionalClient.isPresent()){
            return 1;
        }

        // 身份验证失败
        if(position !=1 && position!=2){
            return 4;
        }

        Client newClient = new Client();
//        newClient.setId();
        newClient.setAccount(account);
        newClient.setPassword(password);
        newClient.setName(name);
        newClient.setAssetValue(0L);
        newClient.setPosition(position);
        LOG.info("newClientId====》》{}",newClient.getId());
        clientRepository.save(newClient);

        return 0;
    }
}
